/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Title 内置可运行线程对象 实现Runnable接口
 * @Description *
 * @Package concurrent.mthread.self
 * @Since 2021/7/29 1:21 下午
 */
package concurrent.mthread.self;

import java.util.concurrent.TimeUnit;

public class InnerRunnable1 {
    private int countDown = 5;
    private Inner inner;

    public InnerRunnable1(String name) {
        this.inner = new Inner(name);
    }

    private class Inner implements Runnable {
        Thread t;

        public Inner(String name) {
            t = new Thread(this, name);
            t.start();
        }

        @Override
        public void run() {
            try {
                for (; ; ) {
                    System.out.println(this);
                    if (--countDown == 0) return;
                    TimeUnit.MILLISECONDS.sleep(10);
                }
            } catch (InterruptedException e) {
                System.err.println(e.getMessage());
            }
        }

        @Override
        public String toString() {
            return t.getName() + ": " + countDown;
        }
    }
}
